﻿using Microsoft.AspNetCore.Identity;
using SecretSourceAPI.Constants;
using SecretSourceAPI.Models;

namespace SecretSourceAPI.Contexts
{
    public class ApplicationDBContextSeed
    {
        public static async Task SeedEssentialsAsync(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
        {
            //初始化种子角色
            await roleManager.CreateAsync(new IdentityRole(Authorization.Roles.Administrator.ToString()));
            await roleManager.CreateAsync(new IdentityRole(Authorization.Roles.Moderator.ToString()));
            await roleManager.CreateAsync(new IdentityRole(Authorization.Roles.User.ToString()));

            //填充种子用户
            var defaultUser = new ApplicationUser {FirstName="guoping",LastName="kong", UserName = Authorization.default_username, Email = Authorization.default_email, EmailConfirmed = true, PhoneNumberConfirmed = true };
            if (!userManager.Users.Any(u => u.Id == defaultUser.Id))
            {
                await userManager.CreateAsync(defaultUser,Authorization.default_password);
                await userManager.AddToRoleAsync(defaultUser, Authorization.default_role.ToString());
            }
        }
    }
}
